package com.cj.service.impl;

import com.cj.dao.DormrepairDao;
import com.cj.po.Dormrepair;
import com.cj.po.PageInfo;
import com.cj.service.DormrepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service("DormrepairService")
    @Transactional
    public class DormrepairServiceImpl implements DormrepairService {

        @Autowired
        private DormrepairDao dormrepairDao;

        @Override
        public PageInfo<Dormrepair> findPageInfo(Integer r_id, Integer d_id, String d_dormbuilding, String r_name, String reason, Date create_time, Date update_time, Integer pageIndex, Integer pageSize) {
            PageInfo<Dormrepair> pi = new PageInfo<Dormrepair>();
            pi.setPageIndex(pageIndex);
            pi.setPageSize(pageSize);
            //获取总条数
            Integer totalCount = dormrepairDao.totalCount(r_id, d_id, d_dormbuilding, r_name, reason, create_time, update_time);
            if (totalCount>0){
                pi.setTotalCount(totalCount);
                //每一页显示学生信息数
                //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
                List<Dormrepair> DormrepairList =dormrepairDao.getDormrepairList(r_id, d_id, d_dormbuilding, r_name, reason, create_time, update_time,
                        (pi.getPageIndex()-1)*pi.getPageSize(),pi.getPageSize());
                pi.setList(DormrepairList);
            }
            return pi;
        }

        @Override
        public List<Dormrepair> getAll() {
            List<Dormrepair> dormrepairList = dormrepairDao.getAll();
            return dormrepairList;
        }

        @Override
        public int deleteDormRepair(Integer r_id) {
            return dormrepairDao.deleteDormRepair(r_id);
        }

        @Override
        public int addDormRepair(Dormrepair dormrepair) {
            dormrepair.setCreate_time(new Date());
            dormrepair.setUpdate_time(new Date());
            return dormrepairDao.addDormRepair(dormrepair);
        }

    @Override
    public int updateDormRepair(Dormrepair dormrepair) {
        return dormrepairDao.updateDormRepair(dormrepair);
    }

    @Override
    public Dormrepair findDormRepairById(Integer r_id) {
        return dormrepairDao.findDormRepairById(r_id);
    }
}
